import { Injectable } from '@nestjs/common';
import { CreateWireDto } from './dto/create-wire.dto';
import { UpdateWireDto } from './dto/update-wire.dto';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class WireService {
  constructor(private prisma: PrismaService) {}
  create(createWireDto: CreateWireDto) {
    return this.prisma.wire.create({
      data: {
        wireName: createWireDto.wireName,
        wireStyle: createWireDto.wireStyle,
        wireWeight: +createWireDto.wireWeight,
        wirePower: +createWireDto.wirePower,
        remark: createWireDto.remark,
      },
    });
  }

  async createMany(createWireDto: CreateWireDto[]) {
    return await this.prisma.wire.createMany({ data: createWireDto });
  }

  async findAll(args: Record<string, any>) {
    const { searchName, searchStyle, page } = args;
    const row = 10;
    let data = [];
    if (page) {
      data = await this.prisma.wire.findMany({
        where: {
          wireName: { contains: searchName },
          wireStyle: { contains: searchStyle },
        },
        skip: (page - 1) * 10,
        take: row,
      });
      return {
        meta: {
          total: await this.prisma.wire.count({
            where: {
              wireName: { contains: searchName },
              wireStyle: { contains: searchStyle },
            },
          }),
          row,
        },
        data,
      };
    } else {
      data = await this.prisma.wire.findMany();
      return {
        meta: {},
        data,
      };
    }
  }

  findOne(id: number) {
    return this.prisma.wire.findFirst({ where: { id } });
  }

  update(id: number, updateWireDto: UpdateWireDto) {
    return this.prisma.wire.update({ where: { id }, data: updateWireDto });
  }

  remove(id: number) {
    return this.prisma.wire.delete({ where: { id } });
  }
}
